<?php

class PersonModel extends aBaseModel
{
	
	protected $dbColumns = array(
		'lw_person' => array(
			'sex' , 'jid' , 'hunger' , 'bodyTemperature' , 'lastEatTime' , 'lastFireTime',
		),
	);
	
	const MAN = 1;
	const WOMAN = 0;
	const ADD = 1;
	const MINUS = 0;
	const WORK_IDLE = 0;
	const WORK_FOOD = 1;
	const WORK_WOOD = 2;
	protected function __construct( $userId , $personId , $lock )
	{
		$db = Common::getDB( $userId );
		$this->data = array();
		$this->data = $db->find( 'lw_person' , array( 'tid' => $personId ) );
		parent::__construct( $userId , 'person_'. $personId , $lock );
	}
	
	public function __destruct()
	{
		$this->save();
		parent::__destruct();
	}
	
	protected function formatToDBData( $table , $data )
	{
		return $data;
	}
	protected function formatFromDBData( $table , $data )
	{
		return $data;
	}
	
	protected function emptyDataWhenloadFromDB( $table )
	{
		return FALSE;
	}
	/**
	 * 
	 * 设置性别
	 * @param field_type $_sex
	 */
	public function setSex( $_sex )
	{
		if( $_sex != Person::MAN || $_sex != Person::WOMAN )
		{
			return false;
		}
		$this->data['sex'] = $_sex;
		$this->updateToDb( 'lw_person', self::DATA_ACTION_ADD , array( 'sex' => $_sex ) );
		return true;
	}
	
	/**
	 * 取得性别
	 * @return self::MAN self::WOMAN
	 */
	public function getSex() 
	{
		return $this->data['sex'];
	}
	
	/**
	 * 砍树
	 * @param Tree $tree  
	 * @return 
	 */
	public function cutTree( $tree )
	{
		if( $tree->beCut() === 0 )
		{
			return TRUE;
		}
		else return FALSE;
	}
	
	/**
	 * 摘果
	 * @param Tree $tree
	 */
	public function getFruit( $tree )
	{
		$tree->getFruit();
	}
    /**
     * 取得体温
     * @return float:
     */
    public function getBodyTemperature ()
    {
        return $this->data['bodyTemperature'];
    }
    
    /**
     * 设置体温
     * @param int $opt  操作  self::ADD  self::MINUS
     * @param float $num  数量
     * @return 操作是否成功
     */
    public function setBodyTemperature( $opt , $num ) 
    {
    	if ( is_float( $num ) && ( $opt == self::ADD || $opt == self::MINUS ) ) 
    	{
    		$tmp = eval(　'$this->data["bodyTemperature"]'.$opt."=$num");
    		$this->updateToDb('lw_person' , self::DATA_ACTION_UPDATE , array( 'bodyTemperature' => $tmp ) );
    		return TRUE;
    	}
    	else return FALSE;
    }

    /**
     * 取得饱腹度
     * @return int:
     */
    public function getHunger ()
    {
        return $this->data['hunger'];
    }
    
    /**
     * 设置饱腹度
     * @param int $opt  操作  self::ADD  self::MINUS
     * @param float $num  数量
     * @return 操作是否成功
     */
    public function setHunger( $opt , $num ) 
    {
    	if ( is_int( $num ) && ( $opt == self::ADD || $opt == self::MINUS ) ) 
    	{
    		$tmp = eval(　'$this->data["bodyTemperature"]'.$opt."=$num");
    		$this->updateToDb('lw_person' , self::DATA_ACTION_UPDATE , array( 'hunger' => $tmp ) );
    		return TRUE;
    	}
    	else return FALSE;
    }

    /**
     * 取得工作
     * @return int:
     */
    public function getJob ()
    {
        return $this->data['jid'];
    }
    
    /**
     * 设置工作
     * @param int job  工作   self::WORK_IDLE  self::WORK_WOOD  self::WORK_FOOD
     * @param float $num  数量
     * @return 操作是否成功
     */
    public function setJob( $job ) 
    {
    	if( $job == self::WORK_IDLE || $job == self::WORK_WOOD || $job == self::WORK_FOOD ) 
    	{
    		$this->data['jid'] = $job;
    		$this->updateToDb('lw_person' , self::DATA_ACTION_UPDATE , array( 'jid' => $job ) );
    		return TRUE;
    	}
    	else return FALSE;
    }
    
    public function getLastEatTime() 
    {
    	return $this->data['lastEatTime'];
    }
    
    public function setLastEatTime()
    {
    	$this->data[$showtime=date("Y-m-d H:i:s")];
    	$this->updateToDb( 'lw_person' , self::DATA_ACTION_UPDATE , array( 'lastEatTime' => $showtime=date("Y-m-d H:i:s") ) );
    }
    /**
     * 丢失体温 饱腹度
     * @return boolean 是否挂了
     */
    public function losePerHour() 
    {
    	if( $this->getJob() == self::WORK_IDLE )
    	{
    		$pastTime = floor( ( time() - $this->data['lastEatTime'] ) / 3600 );
    		$this->data['bodyTemperature'] -=  $pastTime * Common::getConfig('everyhour_consume_bodyTemperature');
    		if ( $this->data['bodyTemperature'] <= 0) $this->data['bodyTemperature'] = 0;
    		$this->data['hunger'] -=  $pastTime * Common::getConfig('everyhour_consume_hunger');
    		if ( $this->data['hunger'] <= 0) $this->data['hunger'] = 0;
      		$this->updateToDb( 'lw_person' , self::DATA_ACTION_UPDATE , array( 'bodyTemperature' => $this->data['bodyTemperature'] ) );
    		$this->updateToDb( 'lw_person' , self::DATA_ACTION_UPDATE , array( 'hunger' => $this->data['hunger'] ) );
    		if( $this->data['bodyTemperature'] < Common::getConfig('die_hunger') || $this->data['hunger'] < Common::getConfig('die_bodyTemperature') )
    		{
    			return true;
    		}
    		if( $this->data['bodyTemperature'] < Common::getConfig('work_bodyTemperature_lowerLimit') || $this->data['hunger'] < Common::getConfig('work_hunger_lowerLimit') )
    		{
    			$this->setJob(self::WORK_IDLE);
    			return false;
    		}
    		return false;
    	}
    	
    }
    
    /**
     * 吃饭烤火
     * @param int $food
     * @param int $wood
     */
    public function personBack( $food , $wood )
    {
		if( is_numeric( $food ) && is_numeric( $wood ))
		{
			$this->data['bodyTemperature'] += $wood * Common::getConfig('every_wood_add_bodytemperature');
			$this->data['hunger'] == $food * Common::getConfig('every_food_add_hunger');
			$this->data['lastFireTime']=time();
			$this->data['lastEatTime'] = $this->data['lastFireTime'];
			if( $this->data['hunger'] >= Common::getConfig('default_hunger') ) $this->data['hunger'] = Common::getConfig('default_hunger');
			$data = array( 'bodyTemperature' => $this->data['bodyTemperature'] , 'hunger' => $this->data['hunger'] , 'lastEatTime' => $this->data['lastEatTime'] , 'lastFireTime' => $this->data['lastFireTime'] );
			$this->updateToDb( 'lw_person' , self::DATA_ACTION_UPDATE , $data );
		}
    }
    
    public function addPerson( $userId ) 
    {
    	$this->updateToDb( 'lw_person' , self::DATA_ACTION_ADD , array( 'uid' => $userId , 
    																	'sex' => Common::computeResult( 50 ) ,
    																	'jid' => self::WORK_IDLE ,
    																	'hunger' => Common::getConfig('default_hunger') ,
    																	'bodyTemperature' => Common::getConfig('default_bodyTemperature') ,
    											 						'lastFireTime' => time(), 
    																	'lastEatTime' => time() ) );
    }													
}
?>